package com.bestcem.xm.qdes.handler.export.type;

import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.qdes.enums.QuestionCustomTypeEnum;
import com.bestcem.xm.qdes.enums.QuestionTypeEnum;
import com.bestcem.xm.qdes.handler.export.BaseTypeHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class BlankHandler implements BaseTypeHandler {

    @Override
    public List<Object> getItems(JSONObject questionDict, JSONObject qgidDict, List<JSONObject> relatedRefs) {
        // 填空题
        List<Object> items = new ArrayList<>();

        String qcid = questionDict.getString("cid");
        String qtitle = questionDict.getString("title");
        String qtype = questionDict.getString("qtype");
        String customQtype = questionDict.getString("custom_qtype");

        int dispSelect = (int) questionDict.getOrDefault("dispSelect", 0);  // dispSelect:0 选项设置; 1 代码设置
        boolean codeSelect = (boolean) questionDict.getOrDefault("codeSelect", true); // true: 显示代码/false: 抹灰代码

        int jumpSelect = (int) questionDict.getOrDefault("jumpSelect", 0);  // jumpSelect:0 选项设置; 1 代码设
        String dispCode = (String) questionDict.getOrDefault("disp_code", "");  // 显示代码
        String jumpCode = (String) questionDict.getOrDefault("jump_code", "");  // 跳转代码
        String autoCode = (String) questionDict.getOrDefault("auto_code", "");  // 自动勾选代码

        String submitOnce = questionDict.getString("submit_once");  // 唯一性校验

        String contentType = questionDict.getString("content_type");  // 开放框内容个数限制
        Object wordsNumberRange = questionDict.get("words_number_range");  // 范围
        boolean openValidate = (Boolean) questionDict.getOrDefault("open_validate", false);  // 开启短信验证

        String inputboxHeight = questionDict.getString("inputbox_height");  // 输入框高度: small, middle, big
        boolean required = (Boolean) questionDict.getOrDefault("required", false);  // 必答

        String typeStr = QuestionTypeEnum.valueOf("QUESTION_TYPE_" + qtype.toUpperCase()).getDesc();
        if (StringUtils.isNotEmpty(customQtype)) {
            try {
                QuestionCustomTypeEnum customQtypeDesc = QuestionCustomTypeEnum.valueOf("QUESTION_CUSTOM_TYPE_" + customQtype.toUpperCase());
                if (customQtypeDesc != null && StringUtils.isNotEmpty(customQtypeDesc.getDesc())) {
                    typeStr = customQtypeDesc.getDesc();
                }
            } catch (IllegalArgumentException exp) {
                log.error("No found QuestionCustomTypeEnum");
            }
        }

        List<String> item0 = new ArrayList<>();
        item0.add(qcid + "[" + typeStr + "]");
        item0.add(qtitle);

        List<String> qAttr = new ArrayList<>();
        qAttr.add(required ? "必答" : "非必答");

        if (StringUtils.isNotEmpty(inputboxHeight)) {
            String inputBoxSize = "";
            if ("small".equals(inputboxHeight)) {
                inputBoxSize = "小";
            } else if ("middle".equals(inputboxHeight)) {
                inputBoxSize = "中";
            } else if ("big".equals(inputboxHeight)) {
                inputBoxSize = "大";
            }
            if (StringUtils.isNotEmpty(inputBoxSize)) {
                qAttr.add(String.format("输入框高度#%s", inputBoxSize));
            }
        }

        String openLimits = openContentLimitTrans(contentType);
        if (StringUtils.isNotEmpty(openLimits)) {
            qAttr.add(openLimits);
        }
        if ("True".equals(submitOnce)) {
            qAttr.add("唯一");
        }
        if (openValidate) {
            qAttr.add("短信验证");
        }
        String rangeTxt = "";

        if (wordsNumberRange instanceof List) {
            List<String> listR = castList(wordsNumberRange, String.class);
            rangeTxt = String.join("-", listR);
        }

        if ("date".equals(contentType) && wordsNumberRange != null) {
            String optionOpenR = wordsNumberRange.toString();
            if ("yyyy".equals(optionOpenR)) {
                rangeTxt = "年";
            } else if ("yyyy-MM".equals(optionOpenR)) {
                rangeTxt = "年-月";
            } else if ("yyyy-MM-dd".equals(optionOpenR)) {
                rangeTxt = "年-月-日";
            }
        }
        if (StringUtils.isNotEmpty(rangeTxt)) {
            qAttr.add(rangeTxt);
        }

        if (dispSelect == 1 && codeSelect) {
            qAttr.add("显示代码");
        } else if (dispSelect == 1 && !codeSelect) {
            qAttr.add("抹灰代码");
        } else {
            qAttr.add("");
        }
        // 题目属性
        List<String> allAttr = new ArrayList<>();
        qAttr.forEach(at -> {
            allAttr.add("[" + at + "]");
        });
        item0.add(String.join("", allAttr));
        // 显示代码，跳转代码，自动勾选代码
        item0.add(dispSelect == 1 ? dispCode : "");
        item0.add(jumpSelect == 1 ? jumpCode : "");
        item0.add(autoCode);
        items.add(item0);

        return items;
    }

    @Override
    public String getType() {
        return "blank";
    }
}
